const db = wx.cloud.database()
const $ = db.command.aggregate
Page({
  data: {
    tabs: ["待使用", "已使用", "已取消"],
    // 默认选中菜单
    currentTab: 0
  },
  //顶部tab切换
  navbarTap: function (e) {
    let index = e.currentTarget.dataset.idx;
    this.setData({
      currentTab: index
    })
    this.getMyOrderList();
  },
  onLoad() {
    this.getMyOrderList()
  },
  getMyOrderList() {
    db.collection('classOrder').orderBy('date', 'asc')
      .where({
        status: this.data.tabs[this.data.currentTab]
      })
      .get()
      .then(res => {
        console.log("我的预约", res)
        this.setData({
          list: res.data
        })
      })
  },
  //确认使用
  makeSure(e) {
    let item = e.currentTarget.dataset.item
    db.collection('classOrder').doc(item._id).update({
      data: {
        status: '已使用'
      }
    }).then(res => {
      wx.showToast({
        title: '确认成功',
      })
      this.getMyOrderList()
    })
  },
  //取消预定
  cancleOrder(e) {
    let item = e.currentTarget.dataset.item
    console.log('item', item)
    wx.cloud.callFunction({
      name: 'classVenue',
      data: {
        action: 'cancel',
        id: item.venueId,
        date: item.date
      }
    }).then(res => {
      console.log('云函数操作结果', res)
      db.collection('classOrder').doc(item._id).update({
        data: {
          status: '已取消'
        }
      }).then(res => {
        wx.showToast({
          title: '取消成功',
        })
        this.getMyOrderList()
      })
    })
  }
})